//#define __FORCE_TRACE__
//#define __FORCE_DEBUG__
//#define __FORCE_INFO__
// handles messages  transfered over the network
// This function will be called inside an Event Handler added to players with XNetAddEvent.
// So when we call the event on one computer, all players get it broadcasted.
// ex: ["my message","all",objNull,"hq"] call XfHandleMessage
//     ["my message","grp",_group,"hq"] call XfHandleMessage

#define THIS_FILE "comm\fn_handleMessage.sqf"
#include <tmf_constants.h>
#include <tmf_macros.h>

TRACE_1("Start",_this);
TMFPROFILERSTART;
private ["_msg","_receiver_type","_receiver","_type","_i"];
_msg = _this select 0;
if (typename _msg == typename "") then {
	_msg = _msg call FFUNC(Translate);
} else {
	for "_i" from 0 to ((count _msg) - 1) step 1 do {
		_msg set [_i, (_msg select _i) call FFUNC(Translate)];
	};
	_msg = format _msg;
};
if (_msg == "") exitWith { ERROR("Message is Empty"); };
_receiver_type = tolower(_this select 1); // "unit", "grp", "all","vec"
_receiver = _this select 2; // only needed for "unit", "grp", "vec", otherwise objNull
_type = tolower(_this select 3); // "global", "vehicle", "side", "group", "hint", "hq"
_sender = player;
if (count _this > 4 && {typename (_this select 4) == typename objNull} && {(_this select 4) isKindOf "Man"}) then { _sender = _this select 4; };

// diag_log Format["XfHandleMessage(): Input: %1",[_msg, _receiver_type, _receiver, _type,(!isNull _receiver),(player == _receiver)]];
if (!ismultiplayer && (IS_DEBUG)) then {
	hint format["DEBUG:\n%1",_msg];
};
switch (_type) do {
	case "global": {
		_msg = _msg call FFUNC(removeLineBreaks);
		switch (_receiver_type) do {
			case "unit": {if (!isNull _receiver) then {if (player == _receiver) then {_msg call FFUNC(GlobalChat)}}};
			case "grp": {if (!isNull _receiver) then {if (player in units _receiver) then {_msg call FFUNC(GlobalChat)}}};
			case "all": {if (isServer && isDedicated) then { DEBUG_1(_type, _msg) } else {_msg call FFUNC(GlobalChat)}};
			case "vec": {if (!isNull _receiver) then {if (player in crew _receiver) then {_msg call FFUNC(GlobalChat)}}};
		};
	};
	case "vehicle": {
		_msg = _msg call FFUNC(removeLineBreaks);
		switch (_receiver_type) do {
			case "unit": {if (!isNull _receiver) then {if (player == _receiver) then {[_sender,_msg] call FFUNC(VehicleChat)}}};
			case "grp": {if (!isNull _receiver) then {if (player in units _receiver) then {[_sender,_msg] call FFUNC(VehicleChat)}}};
			case "vec": {if (!isNull _receiver) then {if (player in crew _receiver) then {[_sender,_msg] call FFUNC(VehicleChat)}}};
		};
	};
	case "side": {
		_msg = _msg call FFUNC(removeLineBreaks);
		switch (_receiver_type) do {
			case "unit": {if (!isNull _receiver) then {if (player == _receiver) then {[_sender,_msg] call FFUNC(SideChat)}}};
			case "grp": {if (!isNull _receiver) then {if (player in units _receiver) then {[_sender,_msg] call FFUNC(SideChat)}}};
			case "all": {if (isServer && isDedicated) then { DEBUG_1(_type, _msg) } else {[_sender,_msg] call FFUNC(SideChat)}};
			case "vec": {if (!isNull _receiver) then {if (player in crew _receiver) then {[_sender,_msg] call FFUNC(SideChat)}}};
		};
	};
	case "system": {
		_msg = _msg call FFUNC(removeLineBreaks);
		switch (_receiver_type) do {
			case "unit": {if (!isNull _receiver) then {if (player == _receiver) then {[_msg] call FFUNC(SystemChat)}}};
			case "grp": {if (!isNull _receiver) then {if (player in units _receiver) then {[_msg] call FFUNC(SystemChat)}}};
			case "all": {if (isServer && isDedicated) then { DEBUG_1(_type, _msg) } else {[_msg] call FFUNC(SystemChat)}};
			case "vec": {if (!isNull _receiver) then {if (player in crew _receiver) then {[_msg] call FFUNC(SystemChat)}}};
		};
	};
	case "group": {
		_msg = _msg call FFUNC(removeLineBreaks);
		switch (_receiver_type) do {
			case "unit": {if (!isNull _receiver) then {if (player == _receiver) then {[player,_msg] call FFUNC(GroupChat)}}};
			case "grp": {if (!isNull _receiver) then {if (player in units _receiver) then {[player,_msg] call FFUNC(GroupChat)}}};
			case "all": {if (isServer && isDedicated) then { DEBUG_1(_type, _msg) } else {[player,_msg] call FFUNC(GroupChat)}};
			case "vec": {if (!isNull _receiver) then {if (player in crew _receiver) then {[player,_msg] call FFUNC(GroupChat)}}};
		};
	};
	case "hint": {
		_msg = _msg call FFUNC(removeLineBreaks);
		switch (_receiver_type) do {
			case "unit": {if (!isNull _receiver) then {if (player == _receiver) then {hintSilent _msg}}};
			case "grp": {if (!isNull _receiver) then {if (player in units _receiver) then {hintSilent _msg}}};
			case "all": {if (isServer && isDedicated) then { DEBUG_1(_type, _msg) } else {hintSilent _msg}};
			case "vec": {if (!isNull _receiver) then {if (player in crew _receiver) then {hintSilent _msg}}};
		};
	};
	case "hq": {
		_msg = _msg call FFUNC(removeLineBreaks);
		switch (_receiver_type) do {
			case "unit": {if (!isNull _receiver) then {if (player == _receiver) then {_msg call FFUNC(HQChat)}}};
			case "grp": {if (!isNull _receiver) then {if (player in units _receiver) then {_msg call FFUNC(HQChat)}}};
			case "all": {if (isServer && isDedicated) then { DEBUG_1(_type, _msg) } else {_msg call FFUNC(HQChat)}};
			case "vec": {if (!isNull _receiver) then {if (player in crew _receiver) then {_msg call FFUNC(HQChat)}}};
		};
	};
	case "titletext": {
		switch (_receiver_type) do {
			case "unit": {if (!isNull _receiver) then {if (player == _receiver) then {_msg call FFUNC(TitleText)}}};
			case "grp": {if (!isNull _receiver) then {if (player in units _receiver) then {_msg call FFUNC(TitleText)}}};
			case "all": {if (isServer && isDedicated) then { DEBUG_1(_type, _msg) } else {_msg call FFUNC(TitleText)}};
			case "vec": {if (!isNull _receiver) then {if (player in crew _receiver) then {_msg call FFUNC(TitleText)}}};
		};
	};
};

TRACE("End");
TMFPROFILERSTOP;